package com.widgets;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.my_view_library.R;


/**
 * 标题控件
 */
public class CustomizeTitleView extends RelativeLayout {

    private int moreTextColor;
    private String titleText;
    private boolean canBack;
    private boolean isText;
    private String backText;
    private String moreText;


    private int titleColor;

    private TextView tvReturn;
    private TextView tvTitle;
    private TextView tvMore;
    private int tvBackImg;
    private int tvMoreImg;

    public CustomizeTitleView(final Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.view_title, this);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomizeTitleView, 0, 0);
        try {
            titleText = ta.getString(R.styleable.CustomizeTitleView_titleText);
            canBack = ta.getBoolean(R.styleable.CustomizeTitleView_canBack, true);
            isText = ta.getBoolean(R.styleable.CustomizeTitleView_isText, true);
            backText = ta.getString(R.styleable.CustomizeTitleView_backText);
            moreText = ta.getString(R.styleable.CustomizeTitleView_moreText);
            tvBackImg = ta.getResourceId(R.styleable.CustomizeTitleView_titleBackImg, R.drawable.empty);
            tvMoreImg = ta.getResourceId(R.styleable.CustomizeTitleView_titleMoreImg, R.drawable.empty);
            titleColor = ta.getColor(R.styleable.CustomizeTitleView_titleColor, context.getResources().getColor(R.color.white));
            moreTextColor = ta.getColor(R.styleable.CustomizeTitleView_moreTextColor, context.getResources().getColor(R.color.white));
            setUpView(context);
        } finally {
            ta.recycle();
        }

        tvReturn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(context instanceof Activity){
                    ((Activity)context).finish();
                }
            }
        });
    }

    private void setUpView(Context context) {
        tvReturn = findViewById(R.id.menu_return);
        tvTitle = findViewById(R.id.title);
        tvTitle.setTextColor(titleColor);
        tvMore = findViewById(R.id.menu_more);
        tvMore.setTextColor(moreTextColor);
        if (!canBack) {
            tvReturn.setVisibility(View.GONE);
        }
        Drawable drawable1 = context.getResources().getDrawable(tvBackImg);
        Drawable drawable2 = context.getResources().getDrawable(tvMoreImg);
        /// 这一步必须要做,否则不会显示.
        drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
        tvReturn.setCompoundDrawables(drawable1, null, null, null);
        tvReturn.setText(backText);
        if (isText) {
            tvMore.setText(moreText);
        } else {
            drawable2.setBounds(0, 0, drawable2.getMinimumWidth(), drawable2.getMinimumHeight());
            tvMore.setCompoundDrawables(drawable2, null, null, null);
        }
        tvTitle.setText(titleText);
    }

    /**
     * 设置标题
     *
     * @param title 标题
     */
    public void setTitle(String title) {
        titleText = title;
        tvTitle.setText(title);
    }

    public void setTitleColor(int titleColor) {
        this.titleColor = titleColor;
        tvTitle.setTextColor(titleColor);
    }


    /**
     * 设置扩展消息
     *
     * @param title 扩展消息
     */
    public void setMoreText(String title) {
        moreText = title;
        tvMore.setText(title);
    }

    public void hideMoreText() {
        tvMore.setVisibility(View.GONE);
    }

    public void hideReturn() {
        tvReturn.setVisibility(View.GONE);
    }

    public void showMoreText() {
        tvMore.setVisibility(View.VISIBLE);
    }

    public TextView getMoreTv(){
        return tvMore;
    }

    /**
     * 设置返回文案
     *
     * @param strReturn 返回文案
     */
    public void setReturnText(String strReturn) {
        backText = strReturn;
        tvReturn.setText(strReturn);
    }

    public void setReturnImg(int backImg) {
        Drawable drawable1 = getContext().getResources().getDrawable(backImg);
        /// 这一步必须要做,否则不会显示.
        drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
        tvReturn.setCompoundDrawables(drawable1, null, null, null);
    }

    /**
     * 设置返回消息事件
     *
     * @param listener 返回消息listener
     */
    public void setReturnListener(OnClickListener listener) {
        tvReturn.setOnClickListener(listener);
    }

    /**
     * 设置扩展事件
     *
     * @param listener 扩展事件listener
     */
    public void setMoreListener(OnClickListener listener) {
        tvMore.setOnClickListener(listener);
    }
}
